package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcStructurationEntity;
import com.dhcc.cdc.busi.service.ICdcStructurationService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 结构化记录
 * 
 * @ClassName: CdcStructurationController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:06
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcstructuration")
public class CdcStructurationController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcstructuration/";

	@Autowired
	private ICdcStructurationService cdcStructurationService;
	

    /**
     * 跳转到结构化记录列表首页
     */
    @RequestMapping("")
    @Permission("cdcstructuration:list")
    public String index() {
        return PREFIX + "cdcstructuration.html";
    }
	
    /**
     * 跳转到新增结构化记录
     */
    @RequestMapping("/to_add")
    @Permission("cdcstructuration:add")
    public String toAdd() {
        return PREFIX + "cdcstructuration_add.html";
    }
	
	/**
     * 跳转到修改结构化记录
     */
    @RequestMapping("/to_update/{cdcStructurationId}")
    @Permission("cdcstructuration:update")
    public String toUpdate(@PathVariable Long cdcStructurationId, Model model) {
        CdcStructurationEntity cdcStructuration = this.cdcStructurationService.selectById(cdcStructurationId);
        model.addAttribute("cdcStructuration",cdcStructuration);
        return PREFIX + "cdcstructuration_edit.html";
    }

    /**
     * 获取结构化记录列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcstructuration:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcStructurationEntity> page = new PageFactory<CdcStructurationEntity>().defaultPage();
        List<CdcStructurationEntity> result = this.cdcStructurationService.page(page,params);
        page.setRecords((List<CdcStructurationEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增结构化记录
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcstructuration:add")
    @BussinessLog(value = "新增结构化记录")
    public Object add(CdcStructurationEntity cdcStructuration) {
        if (CommonUtil.isOneEmpty(cdcStructuration)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcStructuration.insert();
        return R.ok();
    }

    /**
     * 删除结构化记录
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcstructuration:delete")
    @BussinessLog(value = "删除结构化记录")
    public Object delete(@RequestParam Long cdcStructurationId) {
	
        this.cdcStructurationService.deleteById(cdcStructurationId);

        return R.ok();
    }

    /**
     * 修改结构化记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcstructuration:update")
    @BussinessLog(value = "修改结构化记录")
    public Object update(CdcStructurationEntity cdcStructuration) {
        if (CommonUtil.isOneEmpty(cdcStructuration)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcStructurationEntity old = this.cdcStructurationService.selectById(cdcStructuration.getId());

        old.setEpisodeId(cdcStructuration.getEpisodeId());
        old.setOrigchapterId(cdcStructuration.getOrigchapterId());
        old.setToolId(cdcStructuration.getToolId());
        old.setAnalDate(cdcStructuration.getAnalDate());
        old.setIsActive(cdcStructuration.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
